package com.mycom.controller;

import com.mycom.controller.constant.Code;
import com.mycom.controller.entity.Result;
import com.mycom.pojo.Employee;
import com.mycom.pojo.TeamSaveForm2;
import com.mycom.pojo.TeamSaveItem2;
import com.mycom.pojo.TeamWaitCertain;
import com.mycom.service.EmployeeService;
import com.mycom.service.TeamSaveForm2Service;
import com.mycom.service.TeamSaveItem2Service;
import com.mycom.service.TeamWaitCertainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teamSaveForms2")
public class TeamSaveForm2Controller {
    @Autowired
    private TeamSaveForm2Service teamSaveForm2Service;
    @Autowired
    private TeamSaveItem2Service teamSaveItem2Service;
    @Autowired
    private TeamWaitCertainService teamWaitCertainService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 保存团队报表
     * @param teamSaveForm2
     * @return
     */
    @PostMapping
    public Result save(@RequestBody TeamSaveForm2 teamSaveForm2){
        System.out.println(teamSaveForm2);
        Employee employee = employeeService.findByIdCard(teamSaveForm2.getIdCardNumber());
        String formNumber = employee.getDepartmentId() + "-" + employee.getId() + "-" + teamSaveForm2.getSaveDate().getTime();
        teamSaveForm2.setFormNumber(formNumber);
        boolean flag1 = teamSaveForm2Service.save(teamSaveForm2);
        List<TeamSaveItem2> teamSaveItem2s = teamSaveForm2.getSaveItems();
        boolean flag2 = true;
        for (TeamSaveItem2 teamSaveItem2 : teamSaveItem2s) {
            teamSaveItem2.setFormId(teamSaveForm2.getId());
            boolean flag0 = teamSaveItem2Service.save(teamSaveItem2);
            flag2 = flag2 & flag0;
        }
        System.out.println(teamSaveForm2);
        List<TeamWaitCertain> teamWaitCertains = teamSaveForm2.getUsernames();
        boolean flag3 = true;
        for (TeamWaitCertain teamWaitCertain : teamWaitCertains) {
            teamWaitCertain.setFormId(teamSaveForm2.getId());
            teamWaitCertain.setState('0');
            teamWaitCertain.setTeamState('0');
            boolean flag0 = teamWaitCertainService.save(teamWaitCertain);
            flag3 = flag3 & flag0;
        }
        System.out.println(teamWaitCertains);
        boolean flag = flag1 & flag2 & flag3;
        return new Result(flag ? Code.SAVE_SUCCESS : Code.SAVE_ERR,flag);
    }
    @GetMapping("/{idCardNumber}")
    public Result selectFormByIdCard(@PathVariable String idCardNumber){
        List<TeamSaveForm2> teamSaveForm2s = teamSaveForm2Service.findByIdCardNumber(idCardNumber);
        for (int i = teamSaveForm2s.size()-1; i >= 0;i--) {
            TeamSaveForm2 teamSaveForm2 = teamSaveForm2s.get(i);
            List<TeamSaveItem2> teamSaveItem2s = teamSaveItem2Service.findByFormId(teamSaveForm2.getId());
            if (teamSaveItem2s.isEmpty()){
                teamSaveForm2s.remove(i);
            }else {
                List<TeamWaitCertain> usernames = teamWaitCertainService.findByFormId(teamSaveForm2.getId());
                teamSaveForm2.setSaveItems(teamSaveItem2s);
                teamSaveForm2.setUsernames(usernames);
            }
        }
        Integer code = teamSaveForm2s!=null ? Code.QUERY_ALL_SUCCESS : Code.QUERY_ALL_ERR;
        String msg = teamSaveForm2s!=null ? "查询团队待提交报表（按人填报）成功！" : "查询团队待提交报表（按人填报）失败！";
        return new Result(code,teamSaveForm2s,msg);
    }
    @DeleteMapping("/{formNumber}")
    public Result deleteFormByFormNumber(@PathVariable String formNumber){
        TeamSaveForm2 teamSaveForm2 = teamSaveForm2Service.findByFormNumber(formNumber);
        boolean flag1 = teamSaveItem2Service.deleteByFormId(teamSaveForm2.getId());
        boolean flag2 = teamWaitCertainService.deleteByFormId(teamSaveForm2.getId());
        boolean flag = flag1 & flag2;
        if (flag){
            flag = flag & teamSaveForm2Service.deleteByFormNumber(formNumber);
        }
        Integer code = flag ? Code.DELETE_SUCCESS : Code.DELETE_ERR;
        String msg = flag ? "删除团队报表成功！" : "删除团队报表失败！";
        return new Result(code,flag,msg);
    }
    @RequestMapping("/deleteMany")
    public Result deleteMany(@RequestBody String[] formNumbers){
        boolean flag1 = true;
        for (int i = 0; i < formNumbers.length; i++) {
            TeamSaveForm2 teamSaveForm2 = teamSaveForm2Service.findByFormNumber(formNumbers[i]);
            flag1 = flag1 & teamSaveItem2Service.deleteByFormId(teamSaveForm2.getId());
            flag1 = flag1 & teamWaitCertainService.deleteByFormId(teamSaveForm2.getId());
        }
        boolean flag2 = true;
        if (flag1){
            flag2 = teamSaveForm2Service.deleteMany(formNumbers);
        }
        Integer code = flag1 & flag2 ? Code.DELETE_SUCCESS : Code.DELETE_ERR;
        String msg = flag1 & flag2 ? "批量删除团队报表成功！" : "批量删除团队报表失败！";
        return new Result(code,flag1&flag2,msg);
    }
}
